<?
/**
 * This is the main file of the system.
 * 
 * @package TicketsResellersPanel
 */

include_once('../libs/Error/Error.class.php');
include_once('../libs/SendMail/SendMail.class.php');
include('tickets/functions.php');

$test_mode = get_test_mode();

switch($option)
{
	case 'reseller_support':
		if(isset($_POST['Submit']))
			if(add_ticket())
			{
				$success = 1;
			}
		$problemsArr = get_problems();
		$res_email = get_reseller_mail();
		
		if($statusMsg) $currentTab = 4;
		
		ob_start();
		if($success){
			$currentTab = 4;
			$okMsg = "Ticket added successfully!";
			print "<center>";
			include ('okMsg.html');
			print "</center>";
		}else{
			$fileTemplate = 'tickets/add_ticket.html';
			include('tickets/add_ticket.html');
		}
		$add_ticket = ob_get_clean();
		
		$GLOBALS['avg_times'] = array();
		$ticketsArr = get_ticket_list('my_open');
		ob_start();
		$fileTemplate = 'tickets/list.html';
		include('tickets/list.html');
		$my_open = ob_get_clean();
		
		$GLOBALS['avg_times'] = array();
		$ticketsArr = get_ticket_list('my_closed');
		ob_start();
		$fileTemplate = 'tickets/list.html';
		include('tickets/list.html');
		$my_closed = ob_get_clean();
		
		$GLOBALS['avg_times'] = array();
		$ticketsArr = get_ticket_list('my_total');
		ob_start();
		$fileTemplate = 'tickets/list.html';
		include('tickets/list.html');
		$my_total = ob_get_clean();
		
		$fileTemplate = 'tickets/reseller_support.html';
		break;
	case 'client_support':
		$ticketsArr = get_ticket_list('my_clients_open');
		ob_start();
		$fileTemplate = 'tickets/list.html';
		include('tickets/list.html');
		$my_clients_open = ob_get_clean();
		
		$GLOBALS['avg_times'] = array();
		$ticketsArr = get_ticket_list('my_clients_closed');
		ob_start();
		$fileTemplate = 'tickets/list.html';
		include('tickets/list.html');
		$my_clients_closed = ob_get_clean();
		
		$GLOBALS['avg_times'] = array();
		$ticketsArr = get_ticket_list('my_clients_total');
		ob_start();
		$fileTemplate = 'tickets/list.html';
		include('tickets/list.html');
		$my_clients_total = ob_get_clean();
		
		$GLOBALS['avg_times'] = array();
		$ticketsArr = get_ticket_list('my_clients_noanswered');
		ob_start();
		$fileTemplate = 'tickets/list.html';
		include('tickets/list.html');
		$my_clients_noanswered = ob_get_clean();
		
		$GLOBALS['avg_times'] = array();
		$ticketsArr = get_ticket_list('rp_support_noanswered');
		ob_start();
		$fileTemplate = 'tickets/list.html';
		include('tickets/list.html');
		$rp_support_noanswered = ob_get_clean();
		
		$fileTemplate = 'tickets/client_support.html';
		break;
	case 'open_my_ticket':
		if($_POST['do'] == 'submit_comment') insert_comment($tid);
		if($_POST['rateit']) rate_comment($_POST['cid']);
		if($close)
		{
			make_status($tid, 1);
			if(make_status($tid, 1))
			{
				unset($params);
				$params[sid] = $sid;
				$params[action] = 'tickets';
				$params[option] = 'vote';
				$params[tid] = $tid;
				$url = "$baseURL/$webDIR/?".encode_params($params);
				header("Location: ".$url);
				exit;
			}
		}
		if($reopen) make_status($tid, 0);
		$ticket = get_ticket_info($tid);
		$commentsArr = get_ticket_comments($tid);
		$fileTemplate = 'tickets/open_my_ticket.html';
		break;
	case 'open_my_clients_ticket':
		if($_POST['do'] == 'submit_comment') insert_clients_comment($tid);
		if($close)
		{
			make_clients_status($tid, 1);
		}
		if($reopen) make_clients_status($tid, 0);
		$ticket = get_ticket_info($tid);
		$commentsArr = get_ticket_comments($tid);
		$fileTemplate = 'tickets/open_my_clients_ticket.html';
		break;
	case 'vote':
		if(!is_rated($tid))
		{
			if(isset($_POST['RateTicket']))
			{
				if(rate_ticket($tid))
				{
					unset($params);
					$params[sid] = $sid;
					$params[action] = 'tickets';
					$params[option] = 'open_my_ticket';
					$params[tid] = $tid;
					$url = "$baseURL/$webDIR/?".encode_params($params);
					header("Location: ".$url);
					exit;
				}else $GLOBALS['statusMsg'] = 'Already rated!';
			}
			$fileTemplate = 'tickets/vote.html';
		}else{
			$GLOBALS['statusMsg'] = 'Already rated!';
			$fileTemplate = 'tickets/blank.html';
		}
		break;
	case 'download':
		download_attachment($cid, $filename);
		break;
	case 'faq':
		include('tickets/faq.html');
		exit;
	default: $fileTemplate = 'tickets/blank.html';
}


?>